<?php $this->headScript()
->appendFile($this->js('jquery-ui/ui/jquery.ui.datepicker.js'))
->appendFile($this->js('jquery-ui/ui/i18n/ui.datepicker-zh-TW.js'))
->appendFile($this->js('plugins/json.js'))
->captureStart()?>

var weekData = <?php echo $this->weekData ?>;
var weekSetupData = <?php echo $this->weekSetupData ?>;

  $(document).ready(function(){
  	showWeek();

  	<?php if ($this->setWeekFlag): ?>
  		$("#week_setup").click();
  		$("#weekSetup").show();
  	<?php endif ?>

	$("#week_setup").click(function(){
		if (this.checked) {
			$("#weekSetup").show();
		}
		else{
			$("#weekSetup").hide();
		}


	});

		$("#wt tr").click(function(){
			var id = $(this).attr('val');
			if (weekSetupData[id] == 1) {
				weekSetupData[id] = 0;
			}
			else {
				weekSetupData[id] = 1;
			}
			reshoWeek();
		});

		function reshoWeek(){
			var j = 0;
			jQuery.each(weekData,function(i, val){
				if (weekSetupData[i]==1) {
					var x= parseInt(i)-j;
					$("#sel-"+i).parent().css("background", "white");
					$("#sel-"+i).html(x);
					$("#c-"+i).attr("checked", false);
				} else {
					$("#sel-"+i).parent().css("background", "#ccc");
					$("#sel-"+i).html('<?php echo $this->translate('except')?>');
					$("#c-"+i).attr("checked", true);
					j++;
				}
			});
		}

	function showWeek (){
			$("#wt").empty();
			$("#wt").append('<tr bgcolor="#FFF1B8" style="text-align:center;"><td><?php echo $this->translate('except')?></td><td><?php echo $this->translate('Weeks time')?></td><td><?php echo $this->translate('Start Date')?></td></tr>');
			var t ='';
			var j =0;
			jQuery.each(weekData,function(i, val){

				t = "<tr val='"+i+"' ><td><input type='checkbox' id=c-"+i+"></td>";
				var x= parseInt(i)-j;
				if (weekSetupData[i]==0 ) {
					t += '<td id="sel-'+i+'"><?php echo $this->translate('except')?></td>';
					j++;

				} else {
					t += '<td id="sel-'+i+'">'+x+'</td>';
				}
				t += "<td>"+val+"</td></tr>";
				$("#wt").append(t);
			});

		};

	$("#setupBtn").click(function(){
		var aa = [];
		j=1;
		jQuery.each(weekData,function(i, val){
			if (weekSetupData[i]==0) {
				aa[j] = val;
				j++;
			}
		});
		$.post("<?php echo $this->baseScript ?>/setweek",
	  	{ 	year : '<?php echo $this->year?>',
	  		semester : '<?php echo $this->semester?>',
	  		data: [aa] },
	  	function(data){
		    alert("<?php echo $this->translate('configure completed')?> ");
	  	}
		);

	});
	Date.format = 'yyyy-mm-dd';
	$("#started_date").datepicker({dateFormat: 'yy-mm-dd', showOn: 'both',
      		buttonImageOnly: true, buttonImage: '<?php echo $this->img('calendar.gif')?>'});
	$("#ended_date").datepicker({dateFormat: 'yy-mm-dd', showOn: 'both',
      		buttonImageOnly: true, buttonImage: '<?php echo $this->img('calendar.gif')?>'});
	$("#opened_date").datepicker({dateFormat: 'yy-mm-dd', showOn: 'both',
      		buttonImageOnly: true, buttonImage: '<?php echo $this->img('calendar.gif')?>'});
	$("#closed_date").datepicker({dateFormat: 'yy-mm-dd', showOn: 'both',
      		buttonImageOnly: true, buttonImage: '<?php echo $this->img('calendar.gif')?>'});
});

<?php $this->headScript()->captureEnd() ?>
<?php echo $this->headStyle()->captureStart() ?>
#wt{width:100%;margin:2px;}
.maintable{background:#9EBCDD; width:100%; margin:5px;}
.maintable td {padding:3px}
<?php echo $this->headStyle()->captureEnd() ?>

<div id="images"></div>

<form id="myform" method="post" action="">
<table class="maintable">
	<tr bgcolor="#FFFFFF">
	<td style="vertical-align:top;">
		<table cellspacing="0" cellpadding="2">
			<tr bgcolor="#FadFeF"><td><?php echo sprintf($this->translate("configure %d School year %d semester"),$this->year,$this->semester)?></td></tr>
			<tr>
			<td>
			<?php echo $this->form->started_date?>
			</td>
			</tr>
			<tr>
			<td>
			<?php echo $this->form->ended_date?>
			</td>
			</tr>
			<tr>
			<td>
			<?php echo $this->form->opened_date?>
			</td>
			</tr>
			<tr>
			<td>
			<?php echo $this->form->closed_date?>
			</td>
			</tr>
			<tr><td colspan="2">
			<input type="hidden" name="year" value="<?php echo $this->year ?>">
			<input type="hidden" name="semester" value="<?php echo $this->semester ?>">
			<?php echo $this->form->save?>
			</td></tr>
			<tr bgcolor="#FadFeF"><td colspan="2">&nbsp;</td></tr>
		</table>
	</td>
	<td style="vertical-align:top;">
	<table cellspacing="0" cellpadding="3" width="100%">
		<tr bgcolor="#FadFeF">
		<td  align="center">
		<label for="week_setup" style="font-size:small"> <input type="checkbox" id="week_setup" name="week_setup" value="1" >啟用本學期週次設定</label></td>
		</tr>
		<tr><td  style="color:red;text-align:center;">(<?php echo $this->translate('Only need to set up a special situation')?>)</td></tr>
		<tr><td>
		<div id="weekSetup" style="display:none" class="mouse1">

			<table id="wt">
			</table>
			<input type="button" id="setupBtn" value="<?php echo $this->translate('setup')?>" />

		</div>
		</td></tr>
	</table>
	</td></tr>
</table>
</form>